<?php ?>
<div class="middle myclear pad10">
	<p id="bread-crumb">
		<a href="<?php echo site_url('report/earn/?startdate='.$startdate.'&enddate='.$enddate.'&t='.$typead);?>">Tiền kiếm được</a>
		<?php echo $salename;?>
	</p>
	<h3><?php echo $lang['budget_earn']; ?></h3>
    <br />
	<table width="100%">
		<tr>
        	<td class="text-right">
        		<b>Hệ thống</b>
        		<select id="system" onchange="changeSystem();">
        			<option value="-1">- Tất cả -</option>
        			<option value="1" <?php if($typead == 1) echo 'selected="selected"';?>>CPC</option>
        			<option value="2" <?php if($typead == 2) echo 'selected="selected"';?>>CPM</option>
        		</select>
        		&nbsp;|&nbsp;
            	<input type="text" id="dateRange" class="input-time" value="<?php echo $startdate.' / '.$enddate;?>" />
            </td>
        </tr>
	</table>
    <br />
    <table width="100%" cellspacing="0" cellpadding="0" border="0" id="table" class="martop5 tbl-list">
        <tr>
        	<th class="td-label bold text-center" width="5%">
                STT
            </th>
            <th class="td-label bold" width="25%">
                Khách hàng
            </th>
            <th class="td-label bold text-center" width="10%">
                Hệ thống
            </th>
            <th class="td-label bold" width="30%">
                Tiền đã tiêu (VNĐ)
            </th>
			 <th class="td-label bold">
                Tiền Sale được hưởng (VNĐ)
            </th>
		</tr>
		<?php
		$totalExpensed = 0;
		$totalEarn = 0;
		$i = 0;
		if(count($items) > 0)
		{
			foreach ($items as $item){
				$i++;
				$totalExpensed += $item->expensed;
				$totalEarn += $item->earn;
		?>
        <tr class="row">
        	 <td class="text-center">
            	<?php echo $i;?>
            </td>
            <td>
            	<a href="<?php echo site_url('report/earnCustomer/?id='.$item->customerid.'&name='.$item->customername.'&saleid='.$saleid.'&salename='.$salename.'&startdate='.$startdate.'&enddate='.$enddate.'&t='.$typead);?>">
            		<?php echo $item->customername;?>
            	</a>
            </td>
            <td class="text-center">
            	<?php echo ((int)$item->typead == 1 ? 'CPC' : ((int)$item->typead == 2 ? 'CPM' : ''));?>
            </td>
            <td class="text-right">
            	<?php echo number_format($item->expensed);?>
            </td>
            <td class="text-right">
            	<?php echo number_format($item->earn).' <span style="color:#aaa">(~ '.$item->percent.'%)</span>';?>
            </td>
        </tr>
        <?php }?>
        <tr>
        	<td colspan="3" class="td-label bold text-right">Tổng:</td>
        	<td class="td-label bold text-right">
        		<?php echo number_format($totalExpensed);?>
        	</td>
        	<td class="td-label bold text-right">
        		<?php echo number_format($totalEarn).' <span style="color:#aaa">(~ '.round(($totalEarn*100/$totalExpensed), 2).'%)</span>';?>
        	</td>
        </tr>
        <?php } else {?>
        <tr>
        	<td colspan="5">Không có dữ liệu.</td>
        </tr>
        <?php }?>
	</table>
</div>
<script type="text/javascript">
$(document).ready(function(data){
	$('#dateRange').DatePicker({
		format: 'Y-m-d',
		date: ['<?php echo $startdate;?>','<?php echo $enddate;?>'],
		current: '<?php echo date('Y-m-d');?>',
		calendars: 2,
		mode: 'range',
		starts: 1, 
		onChange: function(formated, dates) 
		{
			var str = formated.join(' / ');
			$('#dateRange').val(str);
			$('#dateRange').DatePickerHide();
			window.location.href = '<?php echo site_url('report/earnSale/?id='.$saleid.'&name='.$salename);?>&startdate=' + formated[0] + '&enddate=' + formated[1] + '&t=' + $('#system').val();
		}
	});

	$(function(){
		$('.tbl-list .row').mouseover(function(){
			$(this).css('background','#ffc');
		});
		$('.tbl-list .row').mouseout(function(){
			$(this).css('background','none');
		});
	});
});
function changeSystem()
{
	var formated = $('#dateRange').val().split(" / ");
	window.location.href = '<?php echo site_url('report/earnSale/?id='.$saleid.'&name='.$salename);?>&startdate=' + formated[0] + '&enddate=' + formated[1] + '&t=' + $('#system').val();
}
</script>